package com.alibaba.datax.core.util.geo;

import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.core.util.FrameworkErrorCode;
import com.alibaba.datax.core.util.HttpClientUtil;
import com.alibaba.datax.core.util.geo.response.GeoInfoResponse;
import org.apache.http.client.methods.HttpGet;

import java.net.URL;

/**
 * 地理/逆地理编码
 *
 * @author wangyy
 */
public interface MapGeo {

    /**
     * 地理编码
     *
     * @param address 结构化地址信息
     * @param city    指定查询的城市
     * @return location 坐标点 经度，纬度
     */
    public GeoInfoResponse geo(String address, String city);

    /**
     * 根据location定位街道信息
     *
     * @param location 坐标点 经度，纬度
     * @return
     */
    public GeoInfoResponse regeo(String location);

    /**
     * 基础请求
     *
     * @param uri
     * @return
     */
    default public String get(String uri) {
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        String response = "";
        try {
            URL url = new URL(uri);
            HttpGet httpGet = HttpClientUtil.getGetRequest();
            httpGet.setURI(url.toURI());
            response = httpClientUtil.executeAndGet(httpGet);
        } catch (Exception e) {
            throw DataXException.asDataXException(FrameworkErrorCode.CALL_REMOTE_FAILED, "远程调用失败:" + uri, e);
        }
        return response;
    }
}